package com.hbnu.zhouxunfa;


import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class FileServer {
    public static void main(String[] args) {
        File file = new File("Websocaket\\serverdir\\test.txt");
        try {
            ServerSocket ss = new ServerSocket(8080);
            Socket socket = ss.accept();
            InputStream is = socket.getInputStream();
            FileOutputStream fos = new FileOutputStream(file);
            int len;
            int index=0;
            byte[] bytes = new byte[1024];
            while ((len = is.read())!=-1){
                bytes[index++] = (byte) len;
                fos.write(len);
            }
            System.out.println("服务端打印接收文件的内容："+new String(bytes,0,index));

            //回显数据
            String str = "服务端成功接收";
            OutputStream os = socket.getOutputStream();
            os.write(str.getBytes());

            socket.close();
            ss.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
